<?php

namespace app\common\model;

use think\Model;

/**
 * 会员积分日志模型
 */
class ScoreLog Extends Model
{

    // 表名
    protected $name = 'user_score_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];
    //加积分
    public static function scoreInc($user_id, $score, $momo)
    {
        $user = User::get($user_id);
        if($user){
            $user->score += $score;
            $user->save();
            $log = [
                "user_id" => $user_id,
                "score" => $score,
                "before" => $user["score"] - $score,
                "after" => $user["score"],
                "memo" => $momo,
            ];
            self::create($log);
        } else {
           // log_print($user_id.'用户不存在');
        }
    }

    //减去积分
    public static function scoreDesc($user_id, $score, $momo)
    {
        $user = User::get($user_id);
        if($user && ($user["score"] < $score)){
            $user->score -= $score;
            $user->save();
            $log = [
                "user_id" => $user_id,
                "score" => $score,
                "before" => $user["score"] + $score,
                "after" => $user["score"],
                "memo" => $momo,
            ];
            self::create($log);
        } else {
           // log_print($user_id.'用户不存在');
        }
    }
}
